<?php

namespace app\admin\controller;

use app\common\controller\BaseAdmin;
use app\traits\Result;
use think\facade\Filesystem;
use think\exception\ValidateException;
use think\facade\Config;

class Upload extends BaseAdmin
{

    /**
     * 单张图片上传
     * @return string
     * @throws \Exception
     */
    public function image(){
        $file = request()->file('file');
	    // 上传到本地服务器
        $type = request()->get('type');
	    
	    try {
            validate(['file'=>'fileSize:20480|fileExt:jpg,png,jpeg,gif'])->check($file);
			$savename = Filesystem::disk('public')->putFile($type, $file);
			$path = '/upload/'.$savename;

            //如果是编辑器上传
            if ($type == 'layedit'){
                return Result::error('上传成功',null,['src'=>$path]);
            }
			return Result::success('上传成功',null,$path);
	    } catch (ValidateException $e) {
            return Result::error($e->getMessage());
	    }
    }

}